// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
// import request from "@/utils/request.js";
// import Cache from '@/utils/cache'

// 检查是否超出距离
export function checkIsTrue(data){
    return uni.http({
        url: "order/check_distance",
        method: 'get',
        data,
        noAuth: false,
        requestType:'service',
      })
}

/**
 * 获取用户信息
 *
 */
export const getUserInfo = () => uni.http({
  url: "user",
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/**
 * 头像
 *
 */
export const editAvatar = (data) => uni.http({
  url: "user/change/info",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})

// 修改昵称
export const updateInfo = (data) => uni.http({
  url: "user/change/avatar",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * h5用户登录
 * @param data object 用户账号密码
 */
export const loginH5 = (data) => uni.http({
  url: "auth/login",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * h5用户手机号登录
 * @param data object 用户手机号 也只能
 */
export const loginMobile = (data) => uni.http({
  url: "auth/smslogin",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * h5用户手机号登录
 * @param data object 用户手机号 也只能
 */
export const loginMpPhone = (data) => uni.http({
  url: "auth/mp_phone",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 验证码key
 */
export const getCodeApi = () => uni.http({
  url: "verify_code",
  method: 'get',
  noAuth: false,
  requestType:'service',
})

/**
 * h5用户发送验证码
 * @param data object 用户手机号
 */
export const registerVerify = (data) => uni.http({
  url: "auth/verify",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * h5用户手机号注册
 * @param data object 用户手机号 验证码 密码
 */
export const register = (data) => uni.http({
  url: "auth/register",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})

/**
 * 用户手机号修改密码
 * @param data object 用户手机号 验证码 密码
 */
export const registerReset = (data) => uni.http({
  url: "register/reset",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 用户手机号忘记密码
 */
export const registerForget = (data) => uni.http({
  url: "user/change_pwd",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 获取用户中心菜单
 *
 */
export const getMenuList = (data) => uni.http({
  url: "common/menus",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 签到用户信息
 * */
export const getSignUser = (data) => uni.http({
  url: "user/sign/info",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})

/**
 * 获取签到配置
 *
 */
export const getSignConfig = (data) => uni.http({
  url: "user/sign/config",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 获取签到列表
 * @param object data
 */
export const getSignList = (data) => uni.http({
  url: "user/sign/lst",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 用户签到
 */
export const setSignIntegral = (data) => uni.http({
  url: "user/sign/create",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})

/**
 * 签到列表(年月)
 * @param object data
 *
 */
export const getSignMonthList = (data) => uni.http({
  url: "user/sign/month",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 活动状态
 *
 */
export const userActivity = (data) => uni.http({
  url: "user/activity",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 资金明细（types|0=全部,1=消费,2=充值,3=返佣）
 * */
export const getCommissionInfo = (data) => uni.http({
  url: "user/bill",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 提现列表
 * */
export const extractLst = (data) => uni.http({
  url: "user/extract/lst",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 积分记录
 * */
export const getIntegralList = (data) => uni.http({
  url: "user/integral/lst",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})

/**
 * 获取分销海报图片
 *
 */
export const spreadBanner = () => uni.http({
  url: "user/spread_image",
  method: 'get',
  noAuth: false,
  requestType:'service',
  //#ifdef H5
  data: {
    type: 'wechat'
  }
  //#endif
  //#ifdef MP
  data: {
    type: 'routine'
  }
  //#endif
})
/**
 *
 * 获取推广用户一级和二级
 * @param object data
 */
export const spreadPeople = (data) => uni.http({
  url: "user/spread_list",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 *
 * 推广佣金/提现总和
 * @param int type
 */
export const spreadCount = (type) => uni.http({
  url: "spread/count/" + type,
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/*
 * 推广数据
 * */
export const getSpreadInfo = (data) => uni.http({
  url: "commission",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 *
 * 推广订单
 * @param object data
 */
export const spreadOrder = (data) => uni.http({
  url: "user/spread_order",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 获取推广人排行
 * */
export const getRankList = (data) => uni.http({
  url: "user/spread_top",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 获取佣金排名
 * */
export const getBrokerageRank = (data) => uni.http({
  url: "user/brokerage_top",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 提现申请
 * @param object data
 */
export const extractCash = (data) => uni.http({
  url: "user/extract/create",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 提现银行/提现最低金额
 *
 */
export const extractBank = (data) => uni.http({
  url: "user/extract/banklst",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 会员等级列表
 *
 */
export const userLevelGrade = (data) => uni.http({
  url: "user/level/grade",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 获取某个等级任务
 * @param int id 任务id
 */
export const userLevelTask = (id) => uni.http({
  url: "user/level/task/"+id,
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 检查用户是否可以成为会员
 *
 */
export const userLevelDetection = (data) => uni.http({
  url: "user/level/detection",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 *
 * 地址列表
 * @param object data
 */
export const getAddressList = (data) => uni.http({
  url: "user/address/lst",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 设置默认地址
 * @param int id
 */
export const setAddressDefault = (id) => uni.http({
  url: "user/address/update/"+ id,
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 修改 添加地址
 * @param object data
 */
export const editAddress = (data) => uni.http({
  url: "user/address/create",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 删除地址
 * @param int id
 *
 */
export const delAddress = (id) => uni.http({
  url: "user/address/delete/"+ id,
  method: 'post',
  noAuth: false,
  requestType:'service',
})
/**
 * 获取单个地址
 * @param int id
 */
export const getAddressDetail = (id) => uni.http({
  url: "user/address/detail/"+ id,
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/**
 * 修改用户信息
 * @param object
 */
export const userEdit = (data) => uni.http({
  url: "user/edit",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 退出登录
 * */
export const getLogout = (data) => uni.http({
  url: "logout",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 佣金转入
 *
 */
export const rechargeBrokerage = (data) => uni.http({
  url: "user/recharge/brokerage",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 小程序充值
 *
 */
export const rechargeRoutine = (data) => uni.http({
  url: "recharge/routine",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/*
 * 公众号充值
 * */
export const rechargeWechat = (data) => uni.http({
  url: "user/recharge",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 获取默认地址
 *
 */
export const getAddressDefault = (data) => uni.http({
  url: "address/default",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 充值金额选择
 */
export const getRechargeApi = (data) => uni.http({
  url: "common/recharge_quota",
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 登陆记录
 */
export const setVisit = (data) => uni.http({
  url: "user/set_visit",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 客服列表
 */
export const serviceList = (data) => {
    return uni.http({
        url: "service/list",
        method: 'get',
        data,
        noAuth: false,
        requestType:'service',
      })
}
/**
 * 客服列表
 */
export const serviceLogin = (key, data) => uni.http({
  url: "service/scan_login/" + key,
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 客服获取客户列表
 */
export const serviceUserList = (mer_id, data) => uni.http({
  url: "service/user_list/" + mer_id,
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 用户获取聊天记录详情
 */
export const getChatRecord = (to_uid, data) => uni.http({
  url: "service/history/" + to_uid,
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 客服获取聊天记录详情
 */
export const getMerHistory = (userid, mer_id, data) => uni.http({
  url: "service/mer_history/" + mer_id + '/' + userid,
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 静默绑定推广人
 * @param {Object} puid
 */
export function spread(puid) {
  Cache.set("spread", puid || 0);
  return request.post("user/spread", {
    spread_spid: puid
  });
}
/**
 * 反馈类型
 */
export function feedbackType() {
  return request.get("common/feedback_type");
}
/**
 * 提交反馈
 */
export function feedback(data) {
  return request.post("user/feedback", {
    ...data
  });
}
/**
 * 反馈列表
 */
export function feedbackList(data) {
  return request.get("user/feedback/list", data);
}
/**
 * 反馈列表
 */
export function feedbackDetail(id) {
  return request.get("user/feedback/detail/" + id);
}
/**
 * 浏览记录
 */
export function historyList(data) {
  return request.get("user/history", data);
}
/**
 * 删除浏览记录
 */
export function historyDelete(id) {
  return request.post("user/history/delete/" + id);
}
/**
 * 批量删除浏览记录
 */
export function historyBatchDelete(data) {
  return request.post("user/history/batch/delete", data);
}
/**
 * 批量收藏浏览记录
 */
export function historyBatchCollect(data) {
  return request.post("user/relation/batch/create", data);
}
/**
 * 佣金记录
 */
export function brokerage_list(data) {
  return request.get("user/brokerage_list", data);
}
/**
 * 佣金数据
 */
export function spreadInfo() {
  return request.get("user/spread_info");
}
// 图片验证码
export function getCaptcha() {
  return request.get('captcha', {}, {
    noAuth: true
  });
}
// 用户账户列表
export function userAcc() {
  return request.get('user/account', {}, {
    noAuth: true
  });
}
// 创建发票
export const invoiceSave = (data) => uni.http({
  url: 'user/receipt/create',
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
// 编辑发票
export const invoiceUpdate = (id, data) => uni.http({
  url: 'user/receipt/update/' + id,
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
// 获取默认发票
export const invoiceDefault = (id) => uni.http({
  url: 'user/receipt/is_default/' + id,
  method: 'post',
  noAuth: false,
  requestType:'service',
})
// 发票抬头--列表
export const invoice = (data) => uni.http({
  url: 'user/receipt/lst',
  method: 'get',
  data,
  noAuth: false,
  requestType:'service',
})
// 发票抬头--删除
export const invoiceDelete = (id) => uni.http({
  url: 'user/receipt/delete/' + id,
  method: 'post',
  noAuth: false,
  requestType:'service',
})
// 发票--详情
export const invoiceDetail = (id) => uni.http({
  url: 'user/receipt/detail/' + id,
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/**
 * 新版分享海报信息获取
 *
 */
export function spreadMsg(data) {
  return request.get('user/v2/spread_image', data);
}
/**
 * 图片链接转base64
 *
 */
export const imgToBase = ( data) => uni.http({
  url: "common/base64",
  method: 'post',
  data,
  noAuth: false,
  requestType:'service',
})
/**
 * 获取协议
 *
 */
export const getAgreementApi = ( key) => uni.http({
  url: "agreement/" + key,
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/**
 * 获取协议
 *
 */
export function getIntegralInfo() {
  return request.get('user/integral/info');
}
/**
 * 获取店铺列表
 *
 */
export function getStoreList(data) {
  return request.get('user/services', data);
}
/*
	获取佣金说明
*/
export function commissionDescription() {
  return request.get('agreement/sys_extension_agree')
}
/*
	获取用户分销等级信息
*/
export function getBrokerageInfo() {
  return request.get('user/brokerage/info')
}
/*
	获取用户分销等级表格数据
*/
export function getBrokerageGrade() {
  return request.get('user/brokerage/all')
}
/*
	分销员升级提醒
*/
export function brokerageNotice(data) {
  return request.get(`user/brokerage/notice`, data)
}
/*
	口令解析
*/
export function pwdResolution(data) {
  return request.get(`command/copy?key=${data}`)
}
/*
	获取佣金说明
*/
export function getInstructions(key) {
  return request.get(`agreement/${key}`)
}
/*
	会员信息
*/
export function memberInfo() {
  return request.get('user/member/info')
}
/**
 * 成长值记录
 * @param object data
 *
 */
export function growthValueRecord(data) {
  return request.get('user/member/log', data)
}
/**
 * 协议规则列表
 * @param object data
 *
 */
export function cacheLst() {
  return request.get('agreement_lst', {}, {
    noAuth: true
  })
}
/**
 * 协议规则列表对应的数据
 * @param object data
 *
 */
export function cacheInfo(key) {
  return request.get(`agreement/${key}`, {}, {
    noAuth: true
  })
}
/**
 * 注销账户
 * @param object data
 *
 */
export function userOut(data) {
  return request.post(`user/cancel`, data)
}
/**
 * 获取聊天用户信息
 * @param object data
 *
 */
export function serviceUser(merId, uid) {
  return request.get(`service/user/${merId}/${uid}`)
}
/**
 * 保存聊天用户备注
 * @param object data
 *
 */
export function serviceSaveMark(merId, uid, mark) {
  return request.post(`service/mark/${merId}/${uid}`, {
    mark
  })
}
/**
 * 获取会员卡类型
 * @param object data
 *
 */
export function memberCard() {
  return request.get(`svip/pay_lst`)
}
/**
 * 开通付费会员--支付
 * @param object data
 *
 */
export function memberCardCreate(id, data) {
  return request.post(`svip/pay/${id}`, data)
}
/**
 * 付费会员权益
 * @param object data
 *
 */
export function memberEquity() {
  return request.get(`svip/user_info`, {}, {
    noAuth: true
  })
}
/**
 * 付费会员优惠券
 * @param object data
 *
 */
export function memberCouponLst() {
  return request.get(`svip/coupon_lst`, {}, {
    noAuth: true
  })
}
/**
 * 付费会员优惠券--领取
 * @param object data
 *
 */
export function receiveMemberCoupon(id) {
  return request.post(`svip/coupon_receive/${id}`)
}
/**
 * 付费会员--会员商品
 * @param object data
 *
 */
export function groomList(data) {
  return request.get(`svip/product_lst`, data, {
    noAuth: true
  })
}
/**
 * 客服聊天--撤回消息
 * @param object data
 *
 */
export function chatReverstApi(id) {
  return request.post(`service/recall/${id}`)
}
/**
 * 客服聊天--判断店铺是否有在线客服
 * @param object data
 *
 */
export const hasServiceApi = ( id) => uni.http({
  url: `has_service/${id}`,
  method: 'get',
  noAuth: false,
  requestType:'service',
})
/**
 * 银行卡提现--银行卡信息
 * @param object data
 *
 */
export function getBankInfo() {
  return request.get(`user/extract/history_bank`)
}
let aliVal = {}

const getAliVal = async () => {
  try {
    const res = await uni.http({
      url: 'api/common/oss_sign',
      noAuth: false,
      requestType:'service',
    });
    console.log(res, 22222)
    aliVal = res.data
  } catch (data) {
    aliVal = data
  }
};
export const uploadApi = async (filePaths) => {
  const urls = Array.isArray(filePaths) ? filePaths : [filePaths];

  console.log(urls, 111111)
  if (!aliVal.host || Object.keys(aliVal).length === 0) {
    await getAliVal()
  }

  const uploadPromises = urls.map(uploadFile)

  try {
    const results = await Promise.all(uploadPromises)

    return Array.isArray(filePaths) ? results : results[0]

  } catch (error) {
    throw error

  }

};
